package cn.wolfcode.web.controller;

import cn.wolfcode.qo.MyAJAX;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.qo.QueryObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/permission")
public class PermissionController {
    private IPermissionService permissionService;
    private IDepartmentService departmentService;
    private IRoleService roleService;

    @Autowired
    public void setRoleService(IRoleService roleService) {
        this.roleService = roleService;
    }

    @Autowired
    public void setPermissionService(IPermissionService permissionService) {
        this.permissionService = permissionService;
    }

    @Autowired
    public void setDepartmentService(IDepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    @RequestMapping("/list")
    @RequiredPermission(name="权限页面",expression="permission:list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Permission> pageResult = permissionService.query(qo);
        model.addAttribute("PageInfo",pageResult);
        return "/permission/list";
    }
    @RequestMapping("/delete")
    @RequiredPermission(name="权限删除",expression="permission:delete")
    public String delete(Long id ){
        permissionService.delete(id);
        return "redirect:/permission/list";
    }

    //去到新增页面
    @RequiredPermission(name="权限新增/编辑",expression="permission:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id ,Model model ){
        //把部门数据查出来
        model.addAttribute("role",roleService.selectAll());
        model.addAttribute("departments",departmentService.selectAll());
        if(id != null){
            model.addAttribute("roleSelected",roleService.selectEmpAndRole(id));
            model.addAttribute("permission",permissionService.selectByPrimaryKey(id));
        }
        return  "/permission/input";
    }
    @RequiredPermission(name="权限新增/编辑",expression="permission:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Permission permission,Long[] ids){
        if(permission.getId() != null){
            permissionService.update(permission,ids);
        }else {
            permissionService.insert(permission,ids);
        }
        return "redirect:/permission/list";
    }
    @RequiredPermission(name="权限加载",expression="permission:reload")
    @RequestMapping("/reload")
    @ResponseBody
    public MyAJAX reload(){
            permissionService.reload();
            return new MyAJAX();
    }
}
